package com.youhome.controller;

import com.github.pagehelper.PageInfo;
import com.youhome.model.BClient;
import com.youhome.model.BCounselor;
import com.youhome.model.CounselorRecommendHouse;
import com.youhome.response.Result;
import com.youhome.response.ResultCode;
import com.youhome.response.ResultPage;
import com.youhome.service.ClientService;
import com.youhome.service.CounselorRecommendHouseService;
import com.youhome.service.CounselorService;
import com.youhome.service.HouseService;
import com.youhome.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chen365 on 2018/09/17.
 */
@Api(tags = "推荐房源")
@RestController
@RequestMapping(value = "/clients/")
public class CounselorRecommendHouseController {

    @Autowired
    private CounselorRecommendHouseService counselorRecommendHouseService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private ClientService clientService;
    @Autowired
    private CounselorService counselorService;


    @ApiOperation(value = "列表", notes = "列表", response = CounselorRecommendHouse.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页码", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", required = false, paramType = "query")
    })
    @GetMapping(value = "/{clientId}/houses")
    public Object list(HttpServletRequest request,@PathVariable Long clientId, Integer pageNumber, Integer pageSize) {
        if(StringUtils.isEmpty(clientId)){
            return new ResultPage<>(ResultCode.ERROR_PARAMETER);
        }
        CounselorRecommendHouse record = new CounselorRecommendHouse();

        record.setClientId(clientId);
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor != null) {
            record.setCounselorId(bCounselor.getCounselorId());
        }
        record.setIsDelete(false);
        PageInfo<CounselorRecommendHouse> bClientPageInfo = counselorRecommendHouseService.selectByBean(pageNumber, pageSize, record);
        return new ResultPage<>(ResultCode.SUCCESS, bClientPageInfo);
    }

    @ApiOperation(value = "推荐", notes = "推荐", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "remark", value = "备注", required = false, paramType = "query")
    })
    @PostMapping(value = "/{clientId}/houses")
    public Object house(HttpServletRequest request,@PathVariable Long clientId, String houseId, String remark) {
        if (StringUtils.isEmpty(clientId) || StringUtils.isEmpty(houseId)) {
            return new Result<>(ResultCode.EMPTY_PARAMETER);
        }
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }
        if (!houseService.selectExistsByPrimaryKey(houseId)) {
            return new Result<>(ResultCode.HOUSE_NOT_EXIST);
        }
        if (!clientService.selectExistsByPrimaryKey(clientId)) {
            return new Result<>(ResultCode.CLIENT_NOT_EXIST);
        }
        CounselorRecommendHouse record = new CounselorRecommendHouse();
        record.setCounselorId(bCounselor.getCounselorId());
        record.setClientId(clientId);
        record.setHouseId(houseId);
        record.setIsDelete(false);

        if (!StringUtils.isEmpty(remark)) {
            record.setRemark(remark);
        }
        if (counselorRecommendHouseService.selectExistsByBean(record)) {
            return new Result<>(ResultCode.HAVE_RECOMMENDED);
        }
        if (counselorRecommendHouseService.insert(record) > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }
        return new Result<>(ResultCode.FAIL);
    }


    @ApiOperation(value = "删除", notes = "删除", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @DeleteMapping(value = "/{clientId}/houses/{houseId}")
    public Object delete(HttpServletRequest request,@PathVariable Long clientId, @PathVariable String houseId) {
        if (StringUtils.isEmpty(clientId)) {
            return new Result<>(ResultCode.EMPTY_PARAMETER);
        }
        if (StringUtils.isEmpty(houseId)) {
            return new Result<>(ResultCode.EMPTY_PARAMETER);
        }
        BCounselor bCounselor = counselorService.selectByToken(request);
        if (bCounselor == null) {
            return new Result<>(ResultCode.LAND_EXPIRATION);
        }

        CounselorRecommendHouse record = new CounselorRecommendHouse();

        record.setHouseId(houseId);
        record.setCounselorId(bCounselor.getCounselorId());
        record.setIsDelete(true);

        if (counselorRecommendHouseService.selectExistsByBean(record)) {
            return new Result<>(ResultCode.ALREADY_DELETED);
        }

        if (counselorRecommendHouseService.deleteByBean(record) > 0) {
            return new Result<>(ResultCode.SUCCESS);
        }
        return new Result<>(ResultCode.FAIL);
    }
}
